'''
Created on 18/08/2012

@author: Nrai
'''
# Render Text Example: 'I love pygame!'
import pygame
import sys
from pygame.locals import *
from classes import Grid
from classes import Actor
from sets import Set


block_img = "C://brick.png"
CHARIMG = "C://char.png"

grid = Grid(12, 6)

#block = Sprite(block_img)
hero = Actor(CHARIMG)
hero.move((100,100))

FPS = 30
WWIDTH = 640
WHEIGHT = 480
WSIZE = (WWIDTH,WHEIGHT)

#COLORS
BLACK = (0,0,0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)


pygame.init()

 
screen = pygame.display.set_mode(WSIZE)
pygame.display.set_caption('Test')

fpsClock = pygame.time.Clock()

mainloop = True
circlex = 300
circley = 50
up, down, left, right = False, False, False, False
speed = 1
acc = 1.00
pygame.key.set_repeat(FPS/2, FPS/2)

commandlist = {"dpad" : {
                      "left" : [K_a, K_LEFT],
                      "right" : [K_d, K_RIGHT],
                      "down" : [K_s, K_DOWN],
                      "up" : [K_w, K_UP],
                      },
            "actions" : {
                         "jump" : [K_z], 
                         "run" : [K_x],
                         }
            } 
controls = []
[[controls.extend(key) for key in actions.values()] for actions in commandlist.values()]

pressed_keys = Set([])
prev_frame_keys = Set([])
released_keys = Set([])
held_keys = Set([])

while mainloop:
    
    if pygame.event.get(pygame.QUIT): mainloop = False
    background = [[cell for cell in column] for column in grid.grid]
    xoffset, yoffset = 0, 0
    screen.fill(BLUE)
    #pygame.draw.circle(screen, WHITE, (circlex, circley), 20, 0)
    released_keys = {key.key for key in pygame.event.get(KEYUP)}
    pressed_keys = {key.key for key in pygame.event.get(KEYDOWN)}
    held_keys = held_keys.difference(released_keys)
    held_keys = held_keys.union(pressed_keys)
    held_keys.update([index for index, key in enumerate(pygame.key.get_pressed()) if key])
    
    for key in held_keys:
        if key in commandlist["dpad"]["left"]: xoffset = -10
        elif key in commandlist["dpad"]["right"]: xoffset = +10
        elif key in commandlist["dpad"]["up"]: yoffset = -10
        elif key in commandlist["dpad"]["down"]: yoffset = +10
    hero.move((xoffset, yoffset))
    
    
    fpsClock.tick(FPS)
    allsprites = pygame.sprite.LayeredUpdates()
    allsprites.add(background, layer=0)
    allsprites.add(hero, layer=1)
    allsprites.update()
    allsprites.draw(screen)
    pygame.display.update()
pygame.quit()
